import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 窗体类
 */
public class CreatFrame extends JFrame implements  MouseListener {
    int node_W = 1;
    int node_B = -1;
    int board_size = Static.board_size;
    int Node_w = Static.Node_w;
    boolean isBlack = true;
    boolean gameOver = true;
    private List<Node> board = new ArrayList<>();

    private static Image Img_restart;
    static {
        try {
            Img_restart = ImageIO.read(new File(Static.Src_restart));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public CreatFrame(){
        setTitle("人机对弈五子棋");
        //设置窗体大小
        setSize(Static.Win_W, Static.Win_H);
        //设置窗体颜色
        getContentPane().setBackground(new Color(0, 0, 0));
        //窗体可见
        setVisible(true);
        //居中
        setLocationRelativeTo(null);
        //关闭即结束程序
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //禁止缩放
        setResizable(false);
        setFocusable(true);

        this.repaint();
        this.addMouseListener(this);
    }
    //检查重复棋子
    public int check(Node node, int x, int y, int value){
//        System.out.println(x + " " + y);
        int row = (node.getY() - Static.tool_H) / Node_w;
        int col = (node.getX() - Static.tool_H) / Node_w;


        if (row + x < 0 || col + y < 0 || row + x >= board_size || col + y >= board_size) {
            return 0;
        }
        Node New_node = board.get((row + x) * Static.board_size + (col + y));
        if (New_node.getValue() == value){
            return 1 + check(New_node, x, y, value);
        }
        return 0;
    }

    private boolean checkWin(Node clickedNode) {
        if (clickedNode.getValue() == 0) {
            return false; // 如果点击的位置是空的，则不可能获胜
        }

        int dx[] = {0, -1, -1, -1}; // 水平、垂直、对角线方向的移动量
        int dy[] = {-1, -1, 0, 1};

        int value = clickedNode.getValue();
        for (int i = 0; i < dx.length; i++) {
            int cnt = 1;
            cnt += check(clickedNode, dx[i], dy[i],value ) + check(clickedNode, -dx[i], -dy[i], value);
//            System.out.println(String.format("%d方向的数量为:%d",i, cnt));
            if (cnt == 5){
                return true;
            }
        }
        return false; // 没有找到连续五个相同的棋子
    }

    @Override
    public void mousePressed(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        System.out.println(x);
        System.out.println(y);
        if (gameOver && x >= Static.tool_W && x <= Static.tool_W + board_size * Node_w && y >= Static.tool_H && y <= Static.tool_H + board_size * Node_w){
            int row = (y - Static.tool_H) / Node_w;
            int col = (x - Static.tool_H) / Node_w;
            System.out.println(String.format("第%d行第%d列", row, col));
            int addition = 0;
            addition += (x - Static.tool_H) % Node_w > Node_w / 2 ? 1 : 0;
            addition += (y - Static.tool_H) % Node_w > Node_w / 2 ? Static.board_size : 0;
            Node node = board.get(row * board_size + col + addition);
//            System.out.println(node.getX());
//            System.out.println(node.getY());
//            System.out.println("超过的数量为"+(x - Static.tool_H) % Node_w);
//            System.out.println("超过的数量为"+(y - Static.tool_H) % Node_w);
//            addition += (x - Static.tool_H) % Node_w > Node_w / 2 ? 1 : 0;
//            addition += (y - Static.tool_H) % Node_w > Node_w / 2 ? 15 : 0;
//            System.out.println("增量为:"+addition);
            if (node.clickNode(x, y) && node.getValue() == 0){
                node.setValue(isBlack ? node_B : node_W);
                isBlack = !isBlack;
                if(checkWin(node)){
                    gameOver = false;
                    System.out.println("游戏结束");
                    //弹窗
                    JOptionPane.showMessageDialog(this, "游戏结束！", "五子棋", JOptionPane.INFORMATION_MESSAGE);
                }
                this.repaint();
            }
        }
        else if (x >= Static.Btn_start_W && x <=Static.Btn_end_W && y >=Static.Btn_start_H && y <= Static.Btn_end_H){
            Restart();
        }

    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    private void InitBoard(){
        for (int i = 0; i < Static.board_size * Static.board_size; i++){
//            System.out.println((i / board_size) * Node_w + "," + (i % board_size) * Node_w);
            Node node = new Node( (i % board_size) * Node_w,(i / board_size) * Node_w);
            board.add(node);
        }
    }


    @Override
    public void paint(Graphics g) {
//        //使用双缓冲技术
//        Graphics2D g2d = (Graphics2D) g;
//        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//        //清除背景
//        g2d.setColor(getBackground());
//        g2d.fillRect(0, 0, getWidth(), getHeight());
        if (board.isEmpty()){
            InitBoard();
        }

        for (Node node : board){
            node.drawNode(g);
            if (node.getValue() != 0){
                node.playNode(g);
            }
        }

        g.drawImage(Img_restart, 1000, 400, 50, 50, null);
    }

    public void Restart(){
        for (Node node:board){
            node.setValue(0);
        }
        this.dispose();
        new CreatFrame();
    }
}
